<?php
class LogModel extends Model {
	
	function init(){$this->OpenDB();}
	 
	/**
	 * 获取日志
	 * @param unknown_type $userid 用户ID
	 * @param unknown_type $command_id 指令ID
	 * @param unknown_type $type 显示类型 true|2
	 * @param unknown_type $page
	 * @param unknown_type $limit
	 */
	function getList($userid=null,$command_id=NULL,$type=null,$page=null,$limit=null){
		if (!$userid){$userid=getInf('USER_ID')?getInf('USER_ID'):NULL;} 
		if ($command_id){
			$sql="select * from @#__log where command_id=$command_id  and log_userid=$userid order by log_adtime desc";
		}elseif($type==1){
			$sql="select * from @#__log where log_userid=$userid  order by log_adtime desc";
		}elseif ($type==2){
			$sql="select * from @#__log  order by log_adtime desc";
		}
		else{
			$sql="select * from @#__log where log_userid=$userid and command_id=0 order by log_adtime desc";
		} 
		$count=$this->db->num_rows($this->db->query($sql,'CACHE'));
		if($limit){
			if ($page){
				if($page>ceil($count/$limit))
				$page=$count/$limit;
				elseif ($page<1) {
					$page=1;
				}
				$start=$page*$limit-$limit;
				if ($start<0)$start=0;
			} 
			else 
			$start=0;
			$sql.=" limit $start,$limit";
		} 
		$rt['data']=$this->db->fetchRows($sql,true);
		$rt['count']=$count;
		@$rt['pagecount']=ceil($count/$limit);
		return $rt;
	}
	
function geterrorList($userid=null,$page=null,$limit=null){
		$sql="select * from @#__user INNER JOIN @#__log where @#__log.log_userid=@#__user.user_id and @#__user.user_id=$userid and @#__log.log_result<>'设置密码成功' and  log_result<>'注册成功' order by log_adtime desc";
		$count=$this->db->num_rows($this->db->query($sql,'CACHE'));
		if($limit){
			if ($page){
				if($page>ceil($count/$limit))
				$page=$count/$limit;
				elseif ($page<1) {
					$page=1;
				}
				$start=$page*$limit-$limit;
				if ($start<0)$start=0;
			} 
			else 
			$start=0;
			$sql.=" limit $start,$limit";
		} 
		$rt['data']=$this->db->fetchRows($sql,true);
		$rt['count']=$count;
		@$rt['pagecount']=ceil($count/$limit);
		return $rt;
	}
	
	function getMaoList(){
		return $this->db->fetchRows("select * from @#__mao  order by mao_id desc");
	}
	/**
	 * 删除日志
	 * Enter description here ...
	 * @param unknown_type $id
	 */
	function delLog($id){
	
	}
}